package com.ordersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ordersystem.domain.Dish;
import com.ordersystem.domain.DishRank;
import com.ordersystem.domain.Order;
import com.ordersystem.mappers.DishMapper;
import com.ordersystem.mappers.OrderMapper;
import com.ordersystem.service.DishService;
import com.ordersystem.utils.CommonUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : DishServiceImpl
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/26 14:19]
 * @updateUser : asus1020
 * @updateTime : [2022/3/26 14:19]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private CommonUtils commonUtils;
    @Override//添加菜品
    public int addDish(Dish dish) {
        return dishMapper.addDish(dish);
    }

    @Override//删除菜品
    public int deleteDish(Integer did) {
        return dishMapper.deleteDish(did);
    }

    @Override//修改菜品
    public int updateDish(Dish dish) {
        return dishMapper.updateDish(dish);
    }

    @Override//根据id查询菜品
    public Dish findDishById(Integer did) {
        return dishMapper.findDishById(did);
    }

    @Override//根据条件查询所有菜品
    public Map<String,Object> findDishes(String query, Integer cid, Integer pagenum, Integer pagesize) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Object> pageInfo = PageHelper.startPage(pagenum, pagesize);
        List<Dish> dishes = dishMapper.findDishes(query, cid);
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        map.put("pagenum",pageNum);
        map.put("total",total);
        map.put("dishes",dishes);
        return map;
    }

    @Override//上传菜品图片
    public int saveDishImg(String dishImg, Integer did) {
        return dishMapper.saveDishImg(dishImg,did);
    }

    @Override//查询所有购物车商品
    public List<Dish> findShoppingDish(Integer uid) {
        return dishMapper.findShoppingCar(uid);
    }

    @Override//查询所有订单菜品
    public List<Dish> findOrderDish(String dids) {
        ArrayList<Integer> didsList = new ArrayList<>();
        String[] didsStrs= dids.split(",");
        for (String didsStr : didsStrs) {
            didsList.add(Integer.parseInt(didsStr));
        }
        return dishMapper.findOrderDish(didsList);
    }

    @Override
    public List<Dish> findOrderDishByUidAndDids(Integer uid, String dids) {
        String[] didsStrArray = dids.split(",");
        ArrayList<Integer> didsArray = new ArrayList<>();
        for (String didsStr : didsStrArray) {
            didsArray.add(Integer.parseInt(didsStr));
        }
        return dishMapper.findOrderDishByUidAndDids(didsArray,uid);
    }

    @Override//添加购物车
    public int addShoppingTrolley(Integer uid, Integer did, Integer number) {
        int total = dishMapper.queryShoppingTrolley(uid, did);
        if (total>0){
            return dishMapper.updateShoppingTrolley(uid, did, number);
        }
        return dishMapper.addShoppingTrolley(uid, did, number);
    }

    @Override//根据分类id查询菜品
    public List<Dish> findDishesByCid(Integer cid,Integer uid) {
        return dishMapper.findDishesByCid(cid,uid);
    }

    @Override//删除购物车
    public int deleteShopping(Integer uid, Integer did) {
        return dishMapper.deleteShoppingTrolley(uid, did);
    }

    @Override
    public List<DishRank> getDishRank(Integer rank) {
        return dishMapper.getDishRank(rank);
    }

    @Override
    @Transactional
    //根据订单id查询菜品信息
    public List<Dish> findDishDetailByOid(Integer oid) {
        Order orderByOid = orderMapper.findOrderByOid(oid);
        List gidList = commonUtils.stringToChangeList(orderByOid.getGids(), ",");
        return dishMapper.findDishByGidList(gidList);
    }
}
